package cn.dylan.pet.controller;

import cn.dylan.basic.annotation.PreAuthorize;
import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.PageList;

import cn.dylan.pet.domain.PetType;
import cn.dylan.pet.query.PetTypeQuery;
import cn.dylan.pet.service.IPetTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
public class PetTypeController {
    
    @Autowired
    private IPetTypeService petTypeService;

    /**
     *
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    @PreAuthorize(name = "查询所有部门",sn = "petType:get")
    public List<PetType> loadAll(){
        return petTypeService.loadAll();
    }

    /**
     * GetMapping 请求
     * @requstMapping(value = "", method = "get")
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public PetType loadById(@PathVariable("id") Long id){
        return petTypeService.LoadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个部门" )
    public JsonResult delete(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            petTypeService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 修改和添加
     * @param petType
     * @return
     */
    @PostMapping
    @ApiOperation(value = "修改或者添加" )
    public JsonResult updateOrAdd(@RequestBody PetType petType){
        try {
            if(petType.getId()==null){
                //添加操作
                petTypeService.save(petType);
            }else {
                petTypeService.update(petType);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PutMapping
    @ApiOperation(value = "分页查询" )
    public PageList<PetType> QueryPage(@RequestBody PetTypeQuery query){
        System.out.println(query);
        try {
            return petTypeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            petTypeService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 获取部门树
     * @return
     */
    @GetMapping("/typeTree")
    public List<PetType> deptTree(){
        return petTypeService.typeTree();
    }
    
}
